<?php
/**
* @version		$Id: mod_slg_vm_productslider.php r5 2010-10-24 imranweb7 $
* @copyright	Copyright (C) 2010 imranweb7.com. All rights reserved.
* @license		GNU/GPL
* @Author:      Muhammed Imran Hussain
* @Websites:    http://www.imranweb7.com

* mod_slg_vm_productslider! is a free Joomla+Virtuemart module.
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// check is virtuemart component installed
if(!JComponentHelper::isEnabled('com_virtuemart', true)){
	echo 'VirtueMart software needs to be installed before using this module. Thanks.';
}else{
	jimport( 'joomla.filesystem.file' );
	
	$module_cfg_array = array();
	
	$vm_category = $params->get('categories', array());
	if(is_string($vm_category)){
		$vm_category = array($vm_category);
	}
	$vm_category = array_values($vm_category);
	
	if(count($vm_category) < 1){
		echo 'At leats one virtuemart category should be selected.';
	}else{
		$product_type = $params->get('product_type', 'latest');	
		$module_cfg_array['width'] = $params->get('width', 600);	
		$module_cfg_array['tab_height'] = $params->get('tab_height', 25);	
		$module_cfg_array['content_height'] = $params->get('content_height', 300);	
		$module_cfg_array['tab_item_margin_left'] = $params->get('tab_item_margin_left', 0);	
		$module_cfg_array['tab_item_margin_right'] = $params->get('tab_item_margin_right', 0);	
		$module_cfg_array['tab_group_width'] = $params->get('tab_group_width', 550);	
		$module_cfg_array['max_product'] = $params->get('max_product', 5);	
		$module_cfg_array['max_short_desc_character'] = $params->get('max_short_desc_character', 200);	
		$module_cfg_array['module_theme'] = $params->get('module_theme', 'grey_white');	
		$module_cfg_array['background'] = $params->get('background', '');	
		$module_cfg_array['background_color'] = $params->get('background_color', '#FFFFFF');	
		$module_cfg_array['module_border'] = $params->get('module_border', '1px');	
		$module_cfg_array['module_border_color'] = $params->get('module_border_color', '#000000');	
		$module_cfg_array['force_snapshot_width'] = $params->get('force_snapshot_width', 0);	
		$module_cfg_array['force_thmubs_width'] = $params->get('force_thmubs_width', 100);	
		$module_cfg_array['force_thmubs_height'] = $params->get('force_thmubs_height', 100);	
		$module_cfg_array['thmubs_border'] = $params->get('thmubs_border', '1px');	
		$module_cfg_array['thmubs_border_color'] = $params->get('thmubs_border_color', '#00000');	
		$module_cfg_array['slideshow_timeout'] = $params->get('slideshow_timeout', 2000);	
		$module_cfg_array['slideshow_autoplay'] = $params->get('slideshow_autoplay', 1);	
		$module_cfg_array['slideshow_nav_button'] = $params->get('slideshow_nav_button', 1);
		$module_cfg_array['tab_group_separator'] = $params->get('tab_group_separator', 1);	
		$module_cfg_array['load_mootools'] = $params->get('load_mootools', 1);	
		
		$enable_cache = $params->get('cache', 1);	
		
		$module_cfg_array['module_name'] = $module->module;	
		$module_cfg_array['module_base_path'] = dirname(__FILE__).DS;	
		$module_cfg_array['module_base_uri'] = JURI::base().'modules/'.$module->module.'/';
		
		//content file localtion  
		$html_loc = $module_cfg_array['module_base_path'].'views/default.html';
		$html_url = $module_cfg_array['module_base_uri'].'views/default.html';
		
		require_once ($module_cfg_array['module_base_path'].'helper.php');
		
		if($enable_cache==1) {			
			$conf =& JFactory::getConfig();
			$cache = &JFactory::getCache($module_cfg_array['module_name']);
			$cache->setLifeTime( $params->get( 'cache_time', $conf->getValue( 'config.cachetime' ) * 60 ) );
			$cache->setCaching(true);
			$cache->setCacheValidation(true);
			
			$module_cfg_array['tab_title_list'] = $cache->get( array('ModSliderHelper', 'getVmCategoryNameByIdt'), array($vm_category));
			$module_cfg_array['tab_content'] = $cache->get( array('ModSliderHelper', 'getProductsByCatIds'), array($vm_category, $module_cfg_array['max_product'], $product_type));
			$module_cfg_array['module_css'] = $cache->get( array('ModSliderHelper', 'getModuleCSS'), array($module_cfg_array));
			$module_cfg_array['module_js'] = $cache->get( array('ModSliderHelper', 'getModuleJS'), array($module_cfg_array));
			$html = $cache->get( array('ModSliderHelper', 'getLayoutHtml'), array($module_cfg_array));
		} else {
			$module_cfg_array['tab_title_list'] = ModSliderHelper::getVmCategoryNameById($vm_category);
			$module_cfg_array['tab_content'] = ModSliderHelper::getProductsByCatIds($vm_category, $module_cfg_array['max_product'], $product_type);
			$module_cfg_array['module_css'] = ModSliderHelper::getModuleCSS($module_cfg_array);
			$module_cfg_array['module_js'] = ModSliderHelper::getModuleJS($module_cfg_array);
			$html = ModSliderHelper::getLayoutHtml($module_cfg_array);
		}
		
		// check existing content and new content. write file if different founds
		if (strcasecmp(trim($html), trim(file_get_contents($html_url))) != 0) {
			JFile::write($html_loc, $html);
		}
		
		/*----------LOADING LAYOUT---------*/
		$path = JModuleHelper::getLayoutPath($module_cfg_array['module_name']);
		
		$module_content = '';
		if (JFile::exists($path)) {
			$module_content = JHTML::iframe(
										$html_url, 
										'slg-vm-productscroller-frame', 
										'frameborder="0" width="'.$module_cfg_array['width'].'" height="'.($module_cfg_array['tab_height']+$module_cfg_array['content_height']+5).'" marginheight="0px" marginwidth="0px" scrolling="no"', 
										'<p>Your browser does not support iframes.</p>
									');
			require_once ($path);
		}
	}
}